<?php
/**
 * 设置选项
 *
 * @link https://www.bgbk.org
 */
class Bing_Mpanel {

	/**
	 * 验证随机数 ID
	 *
	 * @var string
	 */
	private $nonce;

	/**
	 * 表单选项样式
	 *
	 * @var object
	 */
	private $panel;

	/**
	 * 数据库字段名
	 *
	 * @var string
	 */
	public $name;

	/**
	 * 构造函数
	 * 初始化对象属性，初始化设置选项和挂载函数到钩子上。
	 */
	function __construct() {
		$this->name  = THEME_MPANEL_NAME;
		$this->nonce = get_stylesheet() . '_mpanel_nonce';

		if ( get_option( $this->name ) === false )
			$this->restore();

		add_action( 'mpanel_content',        array( $this, 'save'                )      );
		add_action( 'wp_ajax_mpanel-save',   array( $this, 'save'                )      );
		add_action( 'mpanel_content',        array( $this, 'enqueue_scripts'     ),   9 );
		add_action( 'admin_menu',            array( $this, 'admin_menu'          )      );
	}

	/**
	 * 获取表单选项
	 */
	function panel() {
		if ( !isset( $this->panel ) ) {
			if( !class_exists( 'Bing_Mpanel_Panel' ) )
				include( dirname( __FILE__ ) . '/mpanel-panel.php' );

			$this->panel = new Bing_Mpanel_Panel( $this );
		}

		return $this->panel;
	}

	/**
	 * 初始化设置
	 */
	function restore() {
		update_option( $this->name, $this->panel()->get_default() );
	}

	/**
	 * 保存设置
	 */
	function save() {
		if ( !(
				( isset( $_GET['page']    ) && $_GET['page']    == 'mpanel'      ) ||
				( isset( $_POST['action'] ) && $_POST['action'] == 'mpanel-save' )
			) ||
			!current_user_can( 'edit_theme_options' ) ||
			!isset( $_POST['mpanel_noncename'] ) ||
			!wp_verify_nonce( $_POST['mpanel_noncename'], $this->nonce )
		)
			return;

		if ( !empty( $_POST['mpanel-restore-settings'] ) ) {
			$this->restore();
			return;
		}

		if ( !empty( $_POST['mpanel-import'] ) ) {
			$import = maybe_unserialize( base64_decode( $_POST['mpanel-import'] ) );

			if ( !empty( $import ) ) {
				update_option( $this->name, $import );
				return;
			}
		}

		if ( isset( $_POST['mpanel'] ) ) {
			$content = $_POST['mpanel'];
			unset( $content['mpanel-export'] );

			update_option( $this->name, $content );
		}

		if ( wp_doing_ajax() )
			wp_die( 1 );
	}

	/**
	 * 创建页面
	 */
	function admin_menu() {
		add_menu_page(
			sprintf( __( '主题设置', 'Bing' ), THEME_NAME ),
			'主题设置',
			'edit_theme_options',
			'mpanel',
			array( $this, 'ui' ),
			'dashicons-awards',
			59
		);

	}

	

	/**
	 * 挂载脚本
	 * 挂载设置选项界面所需要的 CSS 和 JavaScript 脚本。
	 */
	function enqueue_scripts() {
		$url = get_template_directory_uri() . '/mpanel';

		//Switchery
		wp_register_style( 'mpanel-switchery', $url . '/css/switchery.css' );
		wp_register_script( 'mpanel-switchery', $url . '/js/switchery.js' );

		//jQuery UI
		wp_register_style( 'mpanel-jquery-ui', $url . '/css/jquery-ui-1.9.2.custom.css' );
		wp_register_script( 'mpanel-jquery-ui', $url . '/js/jquery-ui-1.9.2.custom.js' );

		//cxCalendar
		wp_register_style( 'mpanel-cxcalendar', $url . '/css/cxcalendar.css', array( 'mpanel-jquery-ui' ) );
		wp_register_script( 'mpanel-cxcalendar', $url . '/js/cxcalendar.js', array( 'mpanel-jquery-ui' ) );

		//Style
		wp_enqueue_style( 'mpanel-style', $url . '/css/style.css', array( 'wp-color-picker', 'mpanel-switchery', 'mpanel-cxcalendar' ), THEME_DB_VERSION );

		//Base
		wp_enqueue_script( 'mpanel-base', $url . '/js/base.js', array( 'jquery', 'wp-color-picker', 'mpanel-switchery', 'mpanel-cxcalendar' ), THEME_DB_VERSION, true );
		wp_localize_script( 'mpanel-base', 'mpanel_base_args', array( 'admin_ajax' => admin_url( 'admin-ajax.php' ), 'list_repeat_error' => __( '列表不能重复', 'Bing' ) ) );

		//Media
		wp_enqueue_media();
	}

	/**
	 * 获取设置
	 */
	function get( $option ) {
		if ( empty( $option ) )
			return false;

		$content = get_option( $this->name );

		if ( !isset( $content[$option] ) )
			return false;

		return wp_unslash( $content[$option] );
	}

	/**
	 * 更新设置
	 */
	function update( $option, $value ) {
		if ( empty( $option ) )
			return false;

		$content = get_option( $this->name );
		$content[$option] = $value;

		return update_option( $this->name, $content );
	}

	/**
	 * 删除设置
	 */
	function delete( $option ) {
		if ( empty( $option ) )
			return false;

		$content = get_option( $this->name );

		if ( !isset( $content[$option] ) )
			return false;

		unset( $content[$option] );

		return update_option( $this->name, $content );
	}

	/**
	 * 生成「主题设置」界面
	 */
	function ui(){
		do_action( 'mpanel_content' );
		$panel = $this->panel()->get_from();
?>
		<div id="mpanel-wrap">
			<form method="post" action="" id="mpanel-form">
				<div class="mpanel-menu-box">
					<h1 class="mpanel-logo"></h1>
					<ul class="mpanel-menu">
						<?php
						$i = 0;
						foreach( $this->panel()->menu as $name ){
							++$i;
							echo "<li class='list-$i'>$name</li>";
						}
						?>
					</ul>
				</div>
				<?php wp_nonce_field( $this->nonce, 'mpanel_noncename' ); ?>
				<input name="mpanel-save" type="submit" class="mpanel-submit mpanel-save" value="<?php esc_attr_e( '保存设置', 'Bing' ); ?>" />
				<div class="mpanel-mian-panel"><?php echo $panel; ?></div>
				<input name="mpanel-save" type="submit" class="mpanel-submit mpanel-save2" value="<?php esc_attr_e( '保存设置', 'Bing' ); ?>" />
			</form>
			<form method="post" action="" id="mpanel-form-restore-settings">
				<input type="submit" name="mpanel-restore-settings" title="<?php esc_attr_e( '恢复初始设置', 'Bing' ); ?>" onClick="return confirm( '<?php echo esc_js( __( '所有目前的设置都会被恢复到初始状态，无法恢复。你确定？', 'Bing' ) ); ?>' ) ? true : false;" value="<?php esc_attr_e( '恢复初始设置', 'Bing' ); ?>" class="mpanel-submit mpanel-restore-settings">
				<?php wp_nonce_field( $this->nonce, 'mpanel_noncename' ); ?>
			</form>
		</div>
		<div id="mpanel-load"></div>
<?php
	}

}

//End of page.
